﻿Public Class InPicture
    Inherits System.Windows.Forms.Form

    Dim homepath As String = Application.UserAppDataPath & "\InPicture"
    Dim appinfo As String = homepath & "\saves.ini"
    Dim locked As Boolean = False
    Dim sizeable As Boolean = False
    Dim saveload As String = ""
    Dim imagesave As String = ""

    Private Declare Function ReleaseCapture Lib "user32" () As Integer
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
        ByVal hwnd As Integer, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByRef lParam As Object) As Integer

    Private Const HTCAPTION As Short = 2
    Private Const WM_NCLBUTTONDOWN As Short = &HA1S
    Private Const WM_SYSCOMMAND As Short = &H112S

    Private Sub InPicture_MouseDown( _
        ByVal eventSender As System.Object, _
        ByVal eventArgs As System.Windows.Forms.MouseEventArgs) _
        Handles Me.MouseDown

        If Not (locked) Then
            ReleaseCapture()
            SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0)
        End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub LockToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LockToolStripMenuItem.Click
        If LockToolStripMenuItem.Text = "Lock" Then
            locked = True
            LockToolStripMenuItem.Text = "Unlock"
        Else
            locked = False
            LockToolStripMenuItem.Text = "Lock"
        End If
    End Sub

    Private Sub ImageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageToolStripMenuItem.Click
        With OpenFileDialog1
            .Filter = _
            "Image (*.jpg, *.png, *.gif)|*.jpg; *.png; *.gif|Alle Dateien (*.*)|*.*"
            .FilterIndex = 1
            .InitialDirectory = "C:\"
            .FileName = ""
            .Title = "Choose an image"
            If .ShowDialog() = DialogResult.OK Then
                Me.BackgroundImage = System.Drawing.Image.FromFile(.FileName)
                imagesave = .FileName.ToString
            End If
        End With
    End Sub

    Private Sub CenterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CenterToolStripMenuItem.Click
        Me.BackgroundImageLayout = ImageLayout.Center
    End Sub

    Private Sub StretchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StretchToolStripMenuItem.Click
        Me.BackgroundImageLayout = ImageLayout.Stretch
    End Sub

    Private Sub ZoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ZoomToolStripMenuItem.Click
        Me.BackgroundImageLayout = ImageLayout.Zoom
    End Sub

    Private Sub TileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileToolStripMenuItem.Click
        Me.BackgroundImageLayout = ImageLayout.Tile
    End Sub

    Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
        Me.Location = New Point(My.Computer.Screen.Bounds.Width / 2 - Me.Size.Width / 2, My.Computer.Screen.Bounds.Height / 2 - Me.Size.Height / 2)
        Me.Size = New Size(250, 250)
        Me.BackgroundImage = Nothing
        Me.BackgroundImageLayout = ImageLayout.None
    End Sub

    Public loctop, locleft As String
    Public leftsize As Boolean = False
    Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
        If sizeable = True Then

            If leftsize Then
                Me.Size = New Size(Cursor.Position.X.ToString - locleft, Cursor.Position.Y.ToString - loctop)
                Me.Location = New Point(locleft, loctop)

                leftsize = False
                sizeable = False

                locked = False
                Me.Opacity = 1

                If saveload <> "" Then
                    Me.BackgroundImage = System.Drawing.Image.FromFile(ReadLine(saveload, 1))
                ElseIf imagesave <> "" Then
                    Me.BackgroundImage = System.Drawing.Image.FromFile(imagesave)
                End If
            End If

            If Not (leftsize) And sizeable Then
                locleft = Cursor.Position.X.ToString
                loctop = Cursor.Position.Y.ToString
                leftsize = True
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TopMost = True
    End Sub

    Private Sub SaveSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSettingsToolStripMenuItem.Click
        TextBox1.Location = New Point(0, 0)
        TextBox1.Visible = True

        If saveload <> "" Then
            TextBox1.Text = saveload
        End If
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            EditLine(homepath & "\" & TextBox1.Text & ".save", imagesave, 1)
            EditLine(homepath & "\" & TextBox1.Text & ".save", Me.Location.X.ToString, 2)
            EditLine(homepath & "\" & TextBox1.Text & ".save", Me.Location.Y.ToString, 3)
            EditLine(homepath & "\" & TextBox1.Text & ".save", Me.Size.Width.ToString, 4)
            EditLine(homepath & "\" & TextBox1.Text & ".save", Me.Size.Height.ToString, 5)
            EditLine(homepath & "\" & TextBox1.Text & ".save", Me.BackgroundImageLayout.ToString, 6)
            TextBox1.Visible = False
            MsgBox("Settings saved.")
        End If
    End Sub

    Private Sub LoadSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadSettingsToolStripMenuItem.Click
        With OpenFileDialog1
            .Filter = _
            "Save (*.save)|*.save"
            .FilterIndex = 1
            .InitialDirectory = homepath
            .Title = "Choose a save"
            .FileName = ""
            If .ShowDialog() = DialogResult.OK Then
                Me.BackgroundImage = System.Drawing.Image.FromFile(ReadLine(.FileName, 1))
                Select Case ReadLine(.FileName, 6)
                    Case "Stretch"
                        Me.BackgroundImageLayout = ImageLayout.Stretch
                    Case "Center"
                        Me.BackgroundImageLayout = ImageLayout.Center
                    Case "Tile"
                        Me.BackgroundImageLayout = ImageLayout.Tile
                    Case "Zoom"
                        Me.BackgroundImageLayout = ImageLayout.Zoom
                End Select
                Me.Location = New Point(ReadLine(.FileName, 2), ReadLine(.FileName, 3))
                Me.Size = New Size(ReadLine(.FileName, 4), ReadLine(.FileName, 5))
                saveload = Replace(.FileName, ".save", "")
                saveload = Replace(.FileName, homepath & "\", "")
                locked = True

                LockToolStripMenuItem.Text = "Unlock"
            End If
        End With
    End Sub

    Private Sub DefineSizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DefineSizeToolStripMenuItem.Click
        Me.Opacity = 0.1
        Me.Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
        Me.Location = New Point(0, 0)
        Me.BackgroundImage = Nothing
        sizeable = True
        locked = True
    End Sub

    Private Sub SetSizeOnImageSizeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetSizeOnImageSizeToolStripMenuItem.Click
        If Me.BackgroundImage.Size <> Nothing Then
            Me.Size = BackgroundImage.Size
        End If
    End Sub

End Class
